package com.d3.pomodoro.component

import android.annotation.SuppressLint
import android.os.CountDownTimer
import android.widget.TextView
import com.orhanobut.logger.Logger
import java.text.DecimalFormat
import kotlin.math.floor


//参数依次为总时长,和计时的时间间隔,要显示的按钮
class TimeCounter(
    millisInFuture: Long,
    countDownInterval: Long,
    textView: TextView? = null,
    textViewExtend: TextView? = null,
    timeCounterListener: TimeCounterListener? = null
) :
    CountDownTimer(millisInFuture, countDownInterval) {

    private var timeCounterListener: TimeCounterListener? = null

    private var millisUntilFinished: Long = 0
    private var millisInFuture: Long = 0
    private var textView: TextView? = null
    private var textViewExtend: TextView? = null

    init {
        this.textView = textView
        this.textViewExtend = textViewExtend
        this.timeCounterListener = timeCounterListener
        this.millisInFuture = millisInFuture
    }

    override fun onFinish() {
        timeCounterListener?.onFinish()
    }

    @SuppressLint("SetTextI18n")
    override fun onTick(millisUntilFinished: Long) {
        this.millisUntilFinished = millisUntilFinished
        val minute = ((millisUntilFinished / 60000).toDouble()).toInt()
        val countTemp = (millisUntilFinished % 60000) / 1000
        val countTemps = if (countTemp < 10) {
            "0$countTemp"
        } else {
            countTemp.toString()
        }
        val minutes = if (minute < 10) {
            "0$minute"
        } else {
            minute.toString()
        }

        textView?.text = "$minutes:$countTemps"
        textViewExtend?.text = "$minutes:$countTemps"
        timeCounterListener?.onTick(millisUntilFinished, millisInFuture)
    }

    interface TimeCounterListener {
        fun onFinish()
        fun onTick(current: Long, total: Long)
    }
}